package com.usian.usianleadonewsuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.article.ApAuthor;
import com.usian.model.article.UserConstants;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.usianleadonewsuser.feing.ArticleFeing;
import com.usian.usianleadonewsuser.feing.WemediaFeing;
import com.usian.usianleadonewsuser.mapper.AuthMapper;
import com.usian.usianleadonewsuser.service.AuthService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.function.Function;

@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper,ApUserRealname> implements AuthService {

    @Resource
    WemediaFeing wemediaFeing;

    @Resource
    ArticleFeing  articleFeing;

    @Override
    public ResponseResult findnameByidpage(AuthDto authDto) {

        //判断参数是否为空
        if(authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //校验参数
        authDto.checkParam();
        //完成分页
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(authDto.getStatus()!=null,ApUserRealname::getStatus,authDto.getStatus());
        Page aa = new Page(authDto.getPage(), authDto.getSize());
        Page page1 = page(aa, queryWrapper);

        //封装参数
        ResponseResult responseResult = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) page1.getTotal());
        responseResult.setData(page1.getRecords());
        return responseResult;
    }

    @GlobalTransactional
    @Override
    public ResponseResult authPass(AuthDto authDto) {
        Integer status=9;
        //判断参数是否为空
        if(authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断状态是否正确
        if(status==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //校验参数
        authDto.checkParam();

        //完成修改状态 创建审核用户实体对象进行修改使用updateById
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setStatus(Short.parseShort(status.toString()));
        apUserRealname.setId(authDto.getId());
        boolean update = updateById(apUserRealname);

        //根据审核用户id用户详细信息
        ApUserRealname byId = getById(authDto.getId());

        //根据审核用户姓名查询是否有媒体用户
        WmUser byName = wemediaFeing.findByName(byId.getName());

        //进行判断是否有账户没有的话就添加
        if(byName==null){
            WmUser wmUser = new WmUser();
            wmUser.setName(byId.getName());
            wmUser.setCreatedTime(new Date());
            wmUser.setStatus(1);
            wmUser.setApUserId(byId.getUserId());
            wmUser.setPassword("123456");
            wemediaFeing.add(wmUser);
        }

        //根据账户表里面的作者id查看作者信息
        WmUser byName1 = articleFeing.findByid(byId.getUserId());

        //防止没有账户第一次查询出来是空的 再进行查询添加作者信息
        WmUser byName2 = wemediaFeing.findByName(byId.getName());

        //判断作者是否是非空的是空的话就先进行创建
        if(byName1==null){
            ApAuthor apAuthor = new ApAuthor();
            apAuthor.setName(byName2.getName());
            apAuthor.setUserId(byName2.getApUserId());
            apAuthor.setCreatedTime(new Date());
            articleFeing.add(apAuthor);
        }
        System.out.println(1/0);
        //添加完毕之后进行封装数据进行返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult authFail(AuthDto authDto) {
        //判断参数是否为空
        if(authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //校验参数
        authDto.checkParam();

        //完成分页
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setStatus(Short.parseShort("2"));
        apUserRealname.setId(authDto.getId());
        boolean update = updateById(apUserRealname);

        //封装参
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }
}
